//
//  ProductListVC.m
//  YangWifi
//
//  Created by zuoxueliangduyuanzhuo on 2017/7/8.
//  Copyright © 2017年 zuoxueliangduyuanzhuo. All rights reserved.
//

#import "ProductListVC.h"
#import "ProductInfoList.h"
#import "ProductListCell.h"
#import "LoginVC.h"
#import "ProductDetailVC.h"
#import "LGLCalenderViewController.h"

@interface ProductListVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation ProductListVC


- (void)loadView
{
    [super loadView];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [self getProductList];

}



- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"packageList" object:nil];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"套餐";
    self.xlTableV.dataSource = self;
    self.xlTableV.delegate = self;
    self.xlTableV.estimatedRowHeight = 200;
    self.xlTableV.rowHeight = UITableViewAutomaticDimension;
    self.xlTableV.tableFooterView = [[UIView alloc] init];
    self.xlTableV.separatorColor = [UIColor clearColor];
    self.xlTableV.backgroundColor = xlHexColor(@"#f4f6f9");
    [self.xlTableV registerNib:[UINib nibWithNibName:@"ProductListCell" bundle:nil] forCellReuseIdentifier:@"ProductListCell"];
    self.noDataImgName = @"";
    self.noDataTitle = @"";
    self.noDataDetailTitle = nil;
    self.btnTitle = nil;
    self.btnBgImgName = nil;
    self.btnBgSelectedImgName = nil;
    
    [self buildNavRightItemWithTitle:@"激活" action:@selector(activateAction:)];

}

- (void)activateAction:(UIButton *)btn
{
    LGLCalenderViewController * ctl = [[LGLCalenderViewController alloc] init];
//    [ctl seleDateWithBlock:^(NSMutableDictionary *paramas) {
//        NSString * date = [NSString stringWithFormat:@"%@-%@-%@", paramas[@"year"], paramas[@"month"], paramas[@"day"]];
//        NSString * price = paramas[@"price"];
//    }];
    [self showViewController:ctl sender:nil];
}

- (void)getProductList
{
    
    
    
    
    NSDictionary *params = @{
                             @"type": @"OSWIFI_PACKAGELIST_MOBILE",
                             @"token":IsNull([AccountHanler token], @""),
                             @"requestEntity":@{
                                     @"telephone":IsNull([AccountHanler telPhone], @"")                                     }
                             };
    @WeakObj(self);
    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
        DLog(@"returnData---->%@",[returnData mj_JSONString]);
        
        XLBaseModel *result = [XLBaseModel mj_objectWithKeyValues:returnData];
        if (result.resultFlag) {
            
            
            
            ProductInfoList *list =[ProductInfoList mj_objectWithKeyValues:returnData];
            
            if (list.responseEntity.count == 0) {
                
                [self.data removeAllObjects];
                self.noDataImgName = @"no_data";
                self.noDataTitle = @"亲，现在没有任何套餐信息哦！";
                self.noDataDetailTitle = @"";
                
                self.btnTitle = nil;
                self.btnBgImgName = nil;
                self.btnBgSelectedImgName = nil;
                
            }else{
                [self.data removeAllObjects];
                self.noDataImgName = @"";
                self.noDataTitle = @"";
                self.noDataDetailTitle = nil;
                
                self.btnTitle = nil;
                self.btnBgImgName = nil;
                self.btnBgSelectedImgName = nil;
                
                [AccountHanler savePackageList:[list mj_JSONString]];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"packageList" object:nil];
                
                [self.data addObjectsFromArray:list.responseEntity];
                [self.xlTableV reloadData];
            }
            
            
            
        }else{
            [Weakself.view makeToast:@"获取产品列表失败"];
            
            self.noDataImgName = @"no_network";
            self.noDataTitle = @"哎呀！系统发生错误";
            self.btnTitle = @"刷新一下试试";
            self.btnBgImgName = @"none_data_btn_bg_normal";
            self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
            self.noDataDetailTitle = nil;
            
        }
        
    } failureBlock:^(NSError *error) {
        DLog(@"error---->%@",error.localizedDescription);
        
        [Weakself.view makeToast:@"获取产品列表失败"];
        [self.data removeAllObjects];
        self.noDataImgName = @"no_network";
        self.noDataTitle = @"哎呀！系统发生错误";
        self.btnTitle = @"刷新一下试试";
        self.btnBgImgName = @"none_data_btn_bg_normal";
        self.btnBgSelectedImgName = @"none_data_btn_bg_selected";
        self.noDataDetailTitle = nil;
        [Weakself.xlTableV reloadData];
        
    } showHUD:NO];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"ProductListCell";
    ProductListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.data = self.data[indexPath.row];
    @WeakObj(self);
    cell.recommendPayHandler = ^(ProductInfoModel *infoModel) {
        [Weakself payHandler:infoModel];
    };
    
    return cell;
}


- (void)payHandler:(ProductInfoModel *)infoModel
{
    if (![AccountHanler isLogin]) {
        
        LoginVC *loginVC = [[LoginVC alloc] initWithNibName:@"LoginVC" bundle:nil];
        loginVC.isNeedOrderNoti = YES;
        loginVC.isNeedHomeNoti = YES;
        @WeakObj(self);
        loginVC.loginHandler = ^{
            [Weakself pushPackageDetail:infoModel];
        };
        XLNavigationVC *nav = [[XLNavigationVC alloc] initWithRootViewController:loginVC];
        [self presentViewController:nav animated:YES completion:^{
            
        }];
        
    }else{
        
        [self pushPackageDetail:infoModel];
        
    }
}


- (void)pushPackageDetail:(ProductInfoModel *)infoModel
{
    
    NSDictionary *params = @{@"type":@"OSWIFI_PACKAGEDETAIL_MOBILE",
                             @"token":IsNull([AccountHanler token], @""),
                             @"requestEntity":@{
                                     @"id": IsNull(infoModel.id, @""),
                                     @"telephone":IsNull([AccountHanler telPhone], @"")}
                             };
    @WeakObj(self);
    [MHNetworkManager postReqeustWithURL:nil params:params successBlock:^(NSDictionary *returnData) {
        
        XLBaseModel *result = [XLBaseModel mj_objectWithKeyValues:returnData];
        if (result.resultFlag) {
            
            ProductDetailVC *vc = [[ProductDetailVC alloc] initWithNibName:@"ProductDetailVC" bundle:nil];
            vc.infoData = [ProductInfoModel mj_objectWithKeyValues:result.responseEntity[0]];
            [self.navigationController showViewController:vc sender:nil];
        }else{
            [Weakself.view makeToast:@"获取产品信息失败"];
        }
        
    } failureBlock:^(NSError *error) {
        [Weakself.view makeToast:@"获取产品信息失败"];
    } showHUD:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

- (void)buttonEvent:(NSString *)title
{
    if ([title isEqualToString:@"刷新一下试试"]){
        [self getProductList];
    }
}


@end
